#-*- coding:utf-8 -*-
import time
import sys
import webbrowser
import os
from multiprocessing import Process

from JsonParserHelper import JsonParserHelper
import dbHelper.MySQLDBHelper
import threading

class DbThread(threading.Thread):
    def __init__(self,dataList):
        threading.Thread.__init__(self)
        self.dataList = dataList
    def run(self):
        mysqlDBhelper = dbHelper.MySQLDBHelper.MySQLDBHelper()

        mysqlDBhelper.initDB()
        # mysqlDBhelper.drop_table()
        # mysqlDBhelper.deleteAllData()
        for i in self.dataList:
            mysqlDBhelper.insertIntoDB(i)
            # mysqlDBhelper.testPrint()
            # mysqlDBhelper.deleteAllData()
        mysqlDBhelper.closeDB()

# def db_run_for_test(dataList):
#     mysqlDBhelper = dbHelper.MySQLDBHelper.MySQLDBHelper()
#
#     mysqlDBhelper.initDB()
#     # mysqlDBhelper.drop_table()
#     # mysqlDBhelper.deleteAllData()
#     for i in dataList:
#         mysqlDBhelper.insertIntoDB(i)
#         # mysqlDBhelper.testPrint()
#         # mysqlDBhelper.deleteAllData()
#     mysqlDBhelper.closeDB()

class Display:
    def __init__(self):
        self.debug = False
        self.path_dir = os.path.dirname(os.path.realpath(__file__))

    def produceWebPage(self):
        debug = self.debug
        path_dir = self.path_dir
        inFileName = path_dir + '/./viewPage/index.html'
        outFileName = path_dir + '/./viewPage/Produced__' + time.strftime("%Y-%m-%d_%H-%M-%S",time.localtime(time.time())) + '.html'
        startStr = '<!--template start-->'
        endStr = '<!--template end-->'
        content = ''
        with open(inFileName) as f:
            content = f.read()
        # f = open(inFileName)
        # content = f.read()
        # f.close()

        startIndex = content.index(startStr)
        endIndex = content.index(endStr)
        
        sHead = content[0:startIndex+len(startStr)]
        sEnd = content[endIndex+len(endStr):]
        # print sEnd

        #TODO:get it later

        sBodyHeaderList = [u'采购人名称',u'项目名称',u'金额',u'时间']
        sBodyHeader = '<thead><tr><th>#</th>'
        #header
        for i in sBodyHeaderList:
            sBodyHeader += '<th>' + str(i) + '</th>'

        sBodyHeader += '</tr></thead>'
        

        jph = JsonParserHelper()
        sBodyBodyList = jph.getJsonList()


        thread_db = DbThread(sBodyBodyList)
        thread_db.start()

        # sBodyBodyList = [[u'邹城市妇幼保健院',u'邹城市妇幼保健院新生儿呼吸机设备',u'270000',u'2013年1月22日'],[u'邹城市妇幼保健院',u'邹城市妇幼保健院新生儿呼吸机设备',u'270000',u'2013年1月22日'],[u'邹城市妇幼保健院',u'邹城市妇幼保健院新生儿呼吸机设备',u'270000',u'2013年1月22日']]
        #td
        sBodyBody = '<tbody>'
        projectNo = 0
        for i in sBodyBodyList:
            sBodyBody += '<tr>'
            sBodyBody += '<td>' + str(projectNo) + '</td>'
            projectNo += 1
            for j in i:
                sBodyBody += '<td>' + str(j) + '</td>'
            sBodyBody += '</tr>'
        sBodyBody += '</tbody>'


        #change to utf-8
        sWhole = sHead + sBodyHeader.decode('utf8') + sBodyBody.decode('utf8') + sEnd
        if(debug):
            print sWhole
        with open(outFileName,'w') as f:
            f.write(sWhole)

        
        child_proc = Process(target=self.showWebBrowser,args=(outFileName,))
        # print ABSPATH+outFileName
        if(not debug):
            child_proc.start()
        # child_proc.join()

        
    def showWebBrowser(self,url):
        print url
        print 'PID# %s' % (os.getpid())
        webbrowser.open(url)
        print webbrowser.get()


if __name__ == '__main__':
    reload(sys)
    sys.setdefaultencoding( "utf-8" )

    display = Display()
    display.produceWebPage()